﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace WooCoo.Web.MVC
{
    /// <summary>
    /// Web的应用程序上下文，变量基于HttpContext存储
    /// </summary>
    public class ApplicationContextManager : IContextManager
    {
        private const string _localContextName = "WooCoo.Web.MVC.LocalContext";
        private const string _clientContextName = "WooCoo.Web.MVC.ClientContext";
        private const string _globalContextName = "WooCoo.Web.MVC.GlobalContext";

        /// <summary>
        /// 当前应用程序是否可用
        /// </summary>
        public bool IsValid
        {
            get { return HttpContext.Current != null; }
        }

        /// <summary>
        /// 取得当前用户授权.
        /// </summary>
        public System.Security.Principal.IPrincipal GetUser()
        {
            return HttpContext.Current.User;
        }

        public void SetUser(System.Security.Principal.IPrincipal principal)
        {
            HttpContext.Current.User = principal;
        }

        public HybridDictionary GetLocalContext()
        {
            return (HybridDictionary)HttpContext.Current.Items[_localContextName];
        }

        public void SetLocalContext(HybridDictionary localContext)
        {
            HttpContext.Current.Items[_localContextName] = localContext;
        }

        public HybridDictionary GetClientContext()
        {
            return (HybridDictionary)HttpContext.Current.Items[_clientContextName];
        }

        public void SetClientContext(HybridDictionary clientContext)
        {
            HttpContext.Current.Items[_clientContextName] = clientContext;
        }

        public HybridDictionary GetGlobalContext()
        {
            return (HybridDictionary)HttpContext.Current.Items[_globalContextName];
        }

        public void SetGlobalContext(HybridDictionary globalContext)
        {
            HttpContext.Current.Items[_globalContextName] = globalContext;
        }
    }
}
